package org.common.utils.fasttest;

import com.alibaba.fastjson2.JSONObject;
import org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap;
import org.common.utils.fasttest.entity.MyAtomicLong;
import org.common.utils.fasttest.util.JacksonUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

/**
 * @author zhouzhibing
 * @date 2024/6/12
 */
public class TestMap {
    private static  final ConcurrentHashMap<String , String> MAP = new ConcurrentHashMap<>();
    static void test() {
//        test1();
//        for (int i = 0; i < 50; i++) {
//            test2();
//            test3();
//        }

//        test4();
//        test5();
        test6();
    }

    private static void test6() {
        Function<Integer , List<Object>> function = (k) -> new ArrayList<>();
        ConcurrentHashMap<Integer , List<Object>> MAP = new ConcurrentHashMap<>();
        Test.doHandler("map.lmable" , 10000000 , 10 , (index) ->{
//            MAP.computeIfAbsent(index , (k) -> new ArrayList<>());
//            MAP.computeIfAbsent(index , ArrayList::new);
            MAP.computeIfAbsent(index , function);
        });
    }

    private static void test5() {
        // 创建一个LinkedHashMap并添加一些元素
        LinkedHashMap<Integer, String> linkedHashMap = new LinkedHashMap<>();
        linkedHashMap.put(1, "One");
        linkedHashMap.put(2, "Two");
        linkedHashMap.put(3, "Three");

        linkedHashMap.reversed().forEach((k , v) -> {
            System.out.println(k + " = " + v);
        });

//        // 使用EntrySet进行倒序遍历
//        for (Map.Entry<Integer, String> entry : linkedHashMap.entrySet()) {
//            System.out.println(entry.getKey() + " = " + entry.getValue());
//        }
    }

    private static void test4() {

        int size = 1000000;
        LinkedHashMap<Integer , Integer> linkedHashMap = new LinkedHashMap<>();
        TreeMap<Integer , Integer> treeMap = new TreeMap<>(Comparator.comparingInt(k -> k));
        for (int i = 1; i <= size; i++) {
            linkedHashMap.put(i , i);
            treeMap.put(i , i);
        }
        Test.doHandler("linkedHashMap" , 1000000 , 10 , (index) ->{
            Integer value = linkedHashMap.get(size);
        });

//        Test.doHandler("treeMap" , 1000000 , 10 , (index) ->{
//            Integer value = treeMap.get(size);
//        });
    }

    private static void test3() {
        ConcurrentHashMap<Integer , MyAtomicLong> map = new ConcurrentHashMap<>(2);
        MyAtomicLong testValue = map.computeIfAbsent(1 ,(k) -> new MyAtomicLong(0));
        new Thread(() -> {
            MyAtomicLong value = map.computeIfAbsent(1 ,(k) -> new MyAtomicLong(0));
            value.incrementAndGet();

//            MyAtomicLong value = map.putIfAbsent(1 , new MyAtomicLong(1));
//            if(value != null)
//                value.incrementAndGet();
        }).start();

        new Thread(() -> {
            MyAtomicLong value = map.computeIfAbsent(1 , (k) -> new MyAtomicLong(0));
            value.incrementAndGet();

//            MyAtomicLong value = map.putIfAbsent(1 , new MyAtomicLong(1));
//            if(value != null)
//                value.incrementAndGet();
        }).start();

        new Thread(() -> {
            MyAtomicLong value = map.computeIfAbsent(1 , (k) -> new MyAtomicLong(0));
            value.incrementAndGet();

//            MyAtomicLong value = map.putIfAbsent(1 , new MyAtomicLong(1));
//            if(value != null)
//                value.incrementAndGet();
        }).start();

        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("test3.map : " + JSONObject.toJSONString(map));
    }


    private static void test2() {
        IntObjectHashMap<String> map = new IntObjectHashMap<>();
        new Thread(() -> {
            map.getIfAbsentPut(1 , "1");
        }).start();

        new Thread(() -> {
            map.getIfAbsentPut(1 , "2");
        }).start();

        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("test2.map : " + JacksonUtils.toJson(map));
    }

    private static void test1() {

        MAP.putIfAbsent("1" , "a");
        MAP.putIfAbsent("1" , "b");

        System.out.println(JSONObject.toJSONString(MAP));
    }
}
